import 'dart:io';

// import 'package:appsflyer_sdk/appsflyer_sdk.dart';
import 'package:bitcoin_mine/api/api.dart';
import 'package:bitcoin_mine/common/global.dart';
import 'package:device_info_plus/device_info_plus.dart';

import '../http/http_request.dart';

class EventLog {
  static var commonParams = {};
  static void init() async {
    if (Platform.isAndroid) {
      AndroidDeviceInfo androidInfo = await DeviceInfoPlugin().androidInfo;
      commonParams["model"] = androidInfo.model;
      commonParams["brand"] = androidInfo.brand;
      commonParams["sys_version"] = androidInfo.version.release;
    } else if (Platform.isIOS) {
      IosDeviceInfo iosInfo = await DeviceInfoPlugin().iosInfo;
      commonParams["model"] = iosInfo.model;
      commonParams["brand"] = "Apple";
      commonParams["sys_version"] = iosInfo.systemVersion;
      commonParams["device_no"] = iosInfo.identifierForVendor;
      commonParams["idfv"] = iosInfo.identifierForVendor;
    }
    commonParams["bundle_id"] = Global.bundleId ?? "";
    commonParams["refer"] = Global.platformRefer;
    commonParams["product_version"] = Global.productVersion ?? "";

    // //todo: 初始化AppsFlyerSDK
    // AppsFlyerOptions appsFlyerOptions = AppsFlyerOptions(
    //   afDevKey: "o7WKU56v2gMY73EQ34cD5g",
    //   appId: "id6754306306",
    //   showDebug: true,
    //   timeToWaitForATTUserAuthorization: 50, // for iOS 14.5
    //   // appInviteOneLink: oneLinkID, // Optional field
    //   disableAdvertisingIdentifier: false, // Optional field
    //   disableCollectASA: false, //Optional field
    //   manualStart: true,
    // ); // Optional field

    // AppsflyerSdk _appsflyerSdk = AppsflyerSdk(appsFlyerOptions);
    // _appsflyerSdk.initSdk(
    //   registerConversionDataCallback: true,
    //   registerOnAppOpenAttributionCallback: true,
    //   registerOnDeepLinkingCallback: true,
    // );

    // // Starting the SDK with optional success and error callbacks
    // _appsflyerSdk.startSDK(
    //   onSuccess: () {
    //     print("AppsFlyer SDK initialized successfully.");
    //   },
    //   onError: (int errorCode, String errorMessage) {
    //     print(
    //       "Error initializing AppsFlyer SDK: Code $errorCode - $errorMessage",
    //     );
    //   },
    // );
  }

  static void log(String eventType, String data) {
    print('EventLog: $eventType, $data');

    HttpRequest.instance.requestNetwork<void>(
      Api.statistic,
      method: "POST",
      params: {...commonParams, "event_type": eventType, "data": data},
      onSuccess: (ignore) {
        print('EventLog: $eventType, $data');
      },
      onError: (error, stackTrace) {
        print('EventLog: $eventType, $data');
      },
    );
  }

  Future<bool?> logEvent(String eventName, Map? eventValues) async {
    bool? result;
    try {
      // result = await appsflyerSdk.logEvent(eventName, eventValues);
    } on Exception {}
    print("Result logEvent: $result");
    return null;
  }
}
